<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- 样式表 -->
    <link rel="stylesheet" href="./css/index.css">
    <!-- 自建弹窗组件 -->
    <script src="./js/myAlert.js"></script>
</head>

<body>
    <form action="./index.html">
        <h3>登录</h3>
        <input type="text" placeholder="账号">
        <input type="password" placeholder="密码">
        <button type="button">登录</button>
    </form>

    <script>
        let btn = document.querySelector('button')
        let input = document.querySelectorAll('input')
        // 给按钮绑定事件
        btn.addEventListener('click', () => {
            if (!input[0].value) {
                myAlert('warning', '账号不能为空')
            } else if (!input[1].value) {
                myAlert('warning', '密码不能为空')
            } else if (input[0].value != user.one.username) {
                myAlert('error', '用户名错误')
            } else if (input[1].value != user.one.password) {
                myAlert('error', '密码错误')
            } else {
                myAlert('success', '登录成功')
                setTimeout(() => {
                    location.pathname = './home.html'
                }, 1500);
            }
        })
        
        //给enter绑定事件
        document.addEventListener('keyup', (e) => {
            if (e.keyCode == 13) {
                if (!input[0].value) {
                    myAlert('warning', '账号不能为空')
                } else if (!input[1].value) {
                    myAlert('warning', '密码不能为空')
                } else if (input[0].value != user.one.username) {
                    myAlert('error', '用户名错误')
                } else if (input[1].value != user.one.password) {
                    myAlert('error', '密码错误')
                } else {
                    myAlert('success', '登录成功')
                    setTimeout(() => {
                        location.pathname = './home.html'
                    }, 1500);
                }
            }else{
                return null
            }
        })

        // 设置用户信息
        let user = {
            'one': { username: 'admin', password: 'admin' }
        }
    </script>
</body>

</html>